/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tanks;

import com.sun.lwuit.Graphics;
import com.sun.lwuit.Image;
import java.io.IOException;

/**
 *
 * @author Александр
 */
public class Explosion {

    int frame = 0;
    int Duration = 12;
    Game game = Game.Instance();
    int strength;
    static Image Gr = null;
    Cell C;
    static Image[] frames = new Image[12]; 
    public Explosion(Cell c, int s) {
        C = c;
        strength = s;
        if(Gr == null){
            try {
                Gr = Image.createImage("/images/explosion.png");
                for(int i =0; i<Duration; i++){
                    frames[i] = Gr.subImage(1, (i * Gr.getWidth()) + 1, Gr.getWidth() - 2, Gr.getWidth() - 2, true);
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public void Draw(Image canvas) {
        if (frame < Duration) {
            Graphics gr = canvas.getGraphics();
            //CurrFrame = Gr.Clone(new RectangleF(1, (frame * Gr.Width)+1, Gr.Width-2, Gr.Width-2), Gr.PixelFormat);                
            gr.drawImage(frames[frame], C.X() * Graph.CellSize(), C.Y() * Graph.CellSize(), Graph.CellSize(), Graph.CellSize());

            frame++;
            if (C.currTank != null && frame < 3) {
                C.currTank.setHealth(C.currTank.Health() - game.rnd.nextInt(strength + 1) - strength);
            }
        } else {
            game.Expl.removeElement(this);
        }
    }
}
